import axios from 'axios'
import { Message } from 'element-ui'
import { removeToken } from '@/assets/js/token'
import router from '@/router'
// import nprogress from 'nprogress'
// import 'nprogress/nprogress.css'


const service = axios.create({
  //baseURL: "http://api.tianapi.com/",
  //baseURL:'https://www.fastmock.site/mock/3ddb36ed19a06abb22b4c7660935eb99/forest/',
  timeout: 6000
})

// 请求拦截
service.interceptors.request.use(config => {
  if (!config.data) {
    config.data = {}
  }

  // config.headers["X-Requested-With"] = 'XMLHttpRequest'
  config.headers["Content-Type"] = 'application/json;charset=UTF-8'
  // config.data.token='11'，
  // config.data.appId='',
  // confi.data.verison=""
  // nprogress.start()
  console.log(config.data)
  return config;
})

// 响应拦截
service.interceptors.response.use(res => {
  if (res.data.code == 200 || res.code == 200) {
    // console.log(res.data)
    return res.data;
  } else if (res.data.code == 401) {
    Message.error('用户名或密码错误,请重新输入!')
  }
  else if (res.data.code == 777) {
    Message.error('用户不存在!')
    removeToken()
    window.location.href = '/'

  }
  else {
    // Message.error(res.data.msg);
    if (res.data.msg.substring(0, 7) == 'Token无效') {
      // console.log('token')
      removeToken()
      Message.error('登录过期请重新登录')
      window.location.href = '/'


    } else {
      Message.error(res.data.msg);
    }
    return Promise.reject(res);
  }
}, err => {
  return Promise.reject(err);
})

//导出
export default service;
